<script>
import ContentFileld from "@/components/ContentFileld.vue";
import {ref} from "vue";
import $ from 'jquery';
import {useRouter} from "vue-router";

export default {
    name: "RegisterIndex",
    components: {
        ContentFileld
    },
    setup() {
        let username = ref("");
        let password = ref("");
        let confirmedPassword = ref("");
        let error_message=ref("");
        let router=useRouter();
        const register = () => {
            console.log("点击")
            $.ajax({
                url: "http://localhost:3000/user/account/register",
                type: "post",
                data: {
                    username: username.value,
                    password: password.value,
                    confirmedPassword: confirmedPassword.value,
                },
                success(resp) {
                    if(resp.message==="success"){
                        router.push({name:"login"});
                    }else{
                        error_message.value=resp.message;
                    }
                }
            })
        }
        return {
            username, password, confirmedPassword,error_message, register,
        }
    }
}
</script>

<template>
    <ContentFileld>
        <div class="row">
            <div class="col"></div>
            <div class="col">
                <form @submit.prevent="register">
                    <div class="mb-3">
                        <label for="username" class="form-label">Username</label>
                        <input v-model="username" type="text" class="form-control" id="username"
                               aria-describedby="emailHelp">
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">Password</label>
                        <input v-model="password" type="password" class="form-control" id="password">
                    </div>
                    <div class="mb-3">
                        <label for="confirmedPassword" class="form-label">confirmedPassword</label>
                        <input v-model="confirmedPassword" type="password" class="form-control" id="confirmedPassword">
                    </div>
                    <div class="error_message">{{ error_message }}</div>
                    <button type="submit" class="btn btn-primary w-100">Submit</button>
                </form>
            </div>
            <div class="col"></div>
        </div>

    </ContentFileld>
</template>

<style scoped>
.error_message{
    color: red;
}
</style>